#!/bin/bash

function info() {
echo Usage: `basename $0` 'list'
exit 1
}

while getopts  ":p:f:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. $var


list=$1

# bed=/mnt/ilustre/app/medical/data/adm/1.bed
# header=/mnt/ilustre/app/medical/data/adm/header

:<<test
for i in `cat $list`; do

    # nam=`basename $i|cut -f1 -d.`
    nam=`echo $i|cut -f2 -d.`
    echo $nam
    
    # samtools view -@4 -f2 -L $bed $i > $out_prefix.$nam.sam
    
    cat $header $out_prefix.$nam.sam > $out_prefix.$nam.sam.1
    mv $out_prefix.$nam.sam.1 $out_prefix.$nam.sam
    
    sort_idx_sam.sh -p$out_prefix.$nam $out_prefix.$nam.sam
    
    rg_add_replace.sh -s$nam -p$out_prefix.$nam $out_prefix.$nam.sort.bam
    
    # ug.sh $out_prefix.$nam.rg.bam
    
    rm $out_prefix.$nam.sort.bam* $out_prefix.$nam.sam

done
test

# for i in `cat $list`; do

    # base=`basename $i .bam`
    # recal0630.sh -l $bed -p$base $i
    # hap.sh -p$base $base.realn.recal.bam $bed
       
# done

num_thread=7

# for out_prefix in 1.5 1.10 1.20; do
# echo admixture
# plink --file $out_prefix --indep-pairwise 1000 50 0.05
# plink --file $out_prefix --extract plink.prune.in --out $out_prefix.filt --make-bed --recode

# out_prefix1=$out_prefix
# out_prefix=${out_prefix}.filt
# for k in `seq 2 12`; do
    # admixture --cv -j$num_thread $out_prefix.bed $k > $out_prefix.$k.log
    # sed -i 's/ /\t/g' $out_prefix.$k.Q
    # paste $out_prefix1.use.sample.list $out_prefix.$k.Q > $out_prefix.$k.q
    
    # str_sort=
    # for j in `seq 2 $k`; do
        # str_sort="$str_sort -k$j,${j}n" 
    # done
    # sort $str_sort $out_prefix.$k.q > $out_prefix.$k.q.sort
    
    # q_bar.py -p$out_prefix.$k $out_prefix.$k.q.sort
# done

# done


# for out_prefix in 1.5 1.10 1.20; do
    # out_prefix=${out_prefix}.filt
    # for k in `seq 2 12`; do
        # echo $out_prefix.$k.q.sort
        # q_bar.py -ptes.$out_prefix.$k $out_prefix.$k.q.sort sample-order.txt
    # done

# done

for i in `seq 22` X Y; do
    
    getw.sh http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/release/20130502/ALL.chr1.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz.tbi
    
    getw.sh http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/release/20130502/ALL.chr1.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz
    
done

. $cmd_done